<template>
  <div style="padding: 10px;">
    <el-row >
      <el-col>
        <div style="display: flex;">
          <div>
            <!--            <el-button type="primary">查询</el-button>-->
            <el-form :model="list_params" ref="searchForm"  size="small" :inline="true" @submit.native.prevent="() => handleQuery()">
              <el-form-item label="" label-width="80px" prop="name">
                <el-input v-model="list_params.name" placeholder="搜索物流公司名称" ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary"  size="mini" native-type="submit">搜索</el-button>
                <el-button  size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div style="flex: 1;"></div>
          <div>
<!--            <el-button>删除</el-button>-->
            <el-button @click="handleEnable(1)" >禁用</el-button>
            <el-button @click="handleEnable(0)" >启用</el-button>
            <!-- <el-button @click="handleExport">导出</el-button>
            <el-button>导入</el-button> -->
            <el-button type="primary" @click="addCustom" >新增</el-button>
          </div>
        </div>
      </el-col>
      <el-col>
        <div style="margin-top: 10px;">
          <el-table :data="table_data" @selection-change="handleSelectionChange" :height="table_height">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="物流公司名称" prop="name"></el-table-column>
            <el-table-column label="负责人" prop="responsiblePerson"></el-table-column>
            <el-table-column label="负责人手机号" prop="responsiblePersonPhonenumber"></el-table-column>
            <el-table-column label="地址" prop="address"></el-table-column>
            <el-table-column label="备注" prop="remark"></el-table-column>
            <el-table-column label="状态" >
              <template slot-scope="scope" >
                <div style="color: #111928;" v-if="scope.row.enable==0" >启用</div>
                <div style="color: #EB4646;" v-else >禁用</div>
              </template>
            </el-table-column>
            <el-table-column label="操作">

              <template slot-scope="scope" >
                <el-button
                  size="mini"
                  type="text"
                  @click="editInfo(scope.row)"

                >编辑</el-button>
                <el-popconfirm
                  title="确定要删除这条记录吗？"
                  @confirm="del_item(scope.row)"
                >
                  <el-button
                    slot="reference"
                    size="mini"
                    type="text"

                  >删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="list_total>0"
            :total="list_total"
            :page.sync="list_params.pageNum"
            :limit.sync="list_params.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>
    <el-dialog  :visible.sync="add_custom_dialog_show" title="添加客户" width="680px" style="border-radius: 10px;" @close="close">
      <el-form  ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入公司名称"  show-word-limit clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="负责人" prop="responsiblePerson">
              <el-input v-model="formData.responsiblePerson" placeholder="请输入负责人"  show-word-limit clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="负责人手机号" prop="responsiblePersonPhonenumber">
              <el-input v-model="formData.responsiblePersonPhonenumber" placeholder="请输入负责人手机号" show-word-limit clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="formData.address" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否启用" prop="enable">
              <el-switch v-model="formData.enable" :active-value="0" :inactive-value="1" ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="title" style="background: #F3F4F6;margin: -20px;padding: 20px;height: 58px;border-radius: 10px;">{{formData.id?'编辑陆运公司':'新增陆运公司'}}</div>
      <div slot="footer" style="display:flex;justify-content: flex-end;">
        <el-button @click="close" >取消</el-button>
        <el-button type="primary"  @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list,add,handleEnable,detail,update,del } from "@/api/base/carriage";
import { getToken } from "@/utils/auth";
import { mapGetters } from 'vuex'
import store from '@/store'
export default {
  name: 'custom',
  dicts:['logistics_type'],
  ...mapGetters([
    'user_id'
  ]),
  data() {
    return {
      table_height:window.innerHeight - 300,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      add_custom_dialog_show:false,
      edit_custom_dialog_show:false,
      table_data:[],
      formData:{
        name:'',
        responsiblePerson:'',
        responsiblePersonPhonenumber:'',
        address:'',
        remark:'',
        enable:1,
        createBy:''
      },
      formDataInit:{
        name:'',
        responsiblePerson:'',
        responsiblePersonPhonenumber:'',
        address:'',
        remark:'',
        enable:1,
        createBy:''
      },
      formEditData:{
        phoneNumber:'',
        customerName:'',
        customerNum:'',
        customerClassify:'',
        enable:1,
        data_enable:true,
        depositBank:'',
        accountName:'',
        account:'',
        pictures:'',
        createBy:''
      },
      pictures:[],
      rules:{
        name:[{
          required: true,
          message: '请输入物流公司名称',
          trigger: 'blur'
        }],
      },
      ids:[],
      list_params:{
        pageNum: 1,
        pageSize: 10,
        name:''
      },
      list_total:0,
    }
  },
  created() {
    this.getList()
  },
  methods:{
    getList(){
      list(this.list_params).then(res => {
        this.table_data = res.rows;
        this.list_total = res.total
        console.log(res.rows)
      })
    },
    addCustom(){
      this.formData = { ...this.formDataInit };
      this.add_custom_dialog_show = true;
      this.$refs.elForm.resetFields();
    },
    close(){
      this.$refs['elForm'].resetFields();
      this.add_custom_dialog_show = false;
    },
    handleConfirm(){

      this.$refs['elForm'].validate((valid)=>{
        if (valid){
          this.formData.createBy = store.getters.user_id;
          console.log(this.formData)

          this.add_custom_dialog_show = false
          if (this.formData.id){
            update(this.formData).then(res=>{
              this.getList()
            })
          }else{
            add(this.formData).then(res=>{
              this.getList()
            })
          }
          this.$refs['elForm'].resetFields();
        }
      });
    },
    handleUpdate(){
      this.$refs['elEditForm'].validate((valid)=>{
        if (valid){
          let formData = this.formEditData;
          formData.createBy = store.getters.user_id;

          this.edit_custom_dialog_show = false
          console.log(formData)
          update(formData).then(res=>{
            this.getList()
          })
          this.$refs['elEditForm'].resetFields();
        }
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleExport() {
      this.download('xnf/customer/export', {
        ...this.queryParams
      }, `customer${new Date().getTime()}.xlsx`)
    },
    handleEnable(enable){
      console.log(this.ids);
      if (this.ids.length == 0){
        this.$message({
          message: '请选择要操作的数据',
          type: 'warning'
        });
        return
      }
      handleEnable(this.ids,enable).then(res => {
        this.getList()
      })
    },
    editInfo(row){
      let id = row.id;
      detail(id).then(res => {
        this.formData = {
          ...res.data,
        };
        this.add_custom_dialog_show = true
      })
    },
    field101BeforeUpload(){},
    handleQuery(){
      this.getList()
    },
    resetQuery() {
      this.$refs.searchForm.resetFields();
      this.list_params = {}
      this.getList()
    },
    del_item({id}){
      del(id).then(res=>{
        this.getList()
      })
    }
  }
}
</script>

<style scoped>

</style>
